﻿$(document).ready(function () {
    pageload();
    $(".headpicbox").click(function () { $(".menubar").toggle(); });
    $("#groupbtn a").each(function () { this.href = this.href + "?openid=" + _baseModel.openid + "&did=" + _baseModel.did + "&gatename=" + _baseModel.gatename; });
    $("#sbgl a").each(function () { this.href = this.href + "?openid=" + _baseModel.openid + "&did=" + _baseModel.did + "&gatename=" + _baseModel.gatename; });
});
var methods = new GetDataJSON();
function pageload() {
    if (_baseModel.gatename == "undefined") { _baseModel.gatename = ""; }
    methods.GetGates(_baseModel.openid, loadGate);
    methods.GetGateStatus(_baseModel.did, loadHommer);
    methods.GetWarningInfo(loadWarningInfo);
    methods.SelectLast(LoadLastInfo);
    loadFamilyInfo();
}
function loadFamilyInfo() {
    var html = "<dt><img src=\"" + _baseModel.userimage + "\"></dt>"
    html += "<dd>" + _baseModel.gatename + "</dd>";

    $("#familyInfo").html(html);
}
function LoadLastInfo(result) {
    function takepills() {
        var takepillsHTML = "<td class=\"tdrights\">吃药时间：</td>";
        if (checkStatus(result.Status013)) {
            var model = result.Status013;
            //takepillsHTML += "<td class=\"tdleft bluetext\">已吃药</td>";
            //takepillsHTML += "<td>" + result.Data013 + "</td>";
            // alert(result.Data013);
            var time = _baseformat.formatTime(result.Data013);

            takepillsHTML += "<td class=\"tdrights bluetext\">" + time + "</td>";
        } else {
            takepillsHTML += "<td class=\"tdrights bluetext\">" + result.Message013 + "</td>";
        }
        $("#takepills").html(takepillsHTML);
    }
    takepills();
    function temperature() {
        var temperatureHTML = "<td class=\"tdrights\">室内温度：</td>";
        if (checkStatus(result.Status014)) {
            var model = result.Data014;
            model[1] = model[1].substring(1,3);
            // console.log(model[1]);
            temperatureHTML += "<td class=\"tdrights bluetext\">" + model[1] + "℃</td>";
            // temperatureHTML += "<td>" + _baseformat.formatTime(model[0]) + "</td>";
        } else {
          // console.log(result);
            temperatureHTML += "<td class=\"tdrights bluetext\">暂无数据</td>";
        }
        $("#temperature").html(temperatureHTML);
    }
    temperature();
    //血压&心率
    function bloodpressureLoad() {
        var bloodpressureHTML = "<td class=\"tdrights\">血压心率：</td>";
        var heartrateHTML = "<td class=\"tdright\">心&nbsp;&nbsp;&nbsp;&nbsp;率：</td>";
        // console.log(result);
        if (checkStatus(result.Status567)) {
            console.log(result.Data567);
            var model = result.Data567;
            var time = _baseformat.formatTime(model[0]);
            model[2] = model[2] < 100 ? model[2].substring(1,3):model[2];
            model[3] = model[3] < 100 ? model[3].substring(1,3):model[3];
            bloodpressureHTML += "<td class=\"tdrights bluetext\">" +model[1] + "/" + model[2] + "&nbsp&nbsp&nbsp" + model[3] + "</td>";
            bloodpressureHTML += "<td  class=\"tdright\">" + '时间&nbsp'+time + "</td>";

            // heartrateHTML += "<td class=\" bluetext\">" + model[3] + "</td>";
            // heartrateHTML += "<td>" + time + "</td>";

        } else {

            bloodpressureHTML += "<td class=\"tdrights bluetext\">暂无数据</td>";
            heartrateHTML += "<td class=\"tdrights bluetext\">暂无数据</td>";
        }
        $("#heartrate").html(heartrateHTML);
        $("#bloodpressure").html(bloodpressureHTML);
    }
    bloodpressureLoad();
}
function loadWarningInfo(result) {
    if (checkStatus(result.Status) && result.Data != null && result.Data.length > 0) {
        $("#menuactive1 img").attr("src", "/img/Index/icon1_1.png");
        var txtWarning = '';
        $(result.Data).each(function () { txtWarning += this + "<br/>"; });
        //$(result.Data).each(function () { txtWarning += "<div class=\"warningbox\">" + this + "</div>"; });
        $("#textWarning").html(txtWarning).show();
        $("#menuactive1").addClass("menuactive1");
    }
}
function loadGate(data) {
    var html = '';
    if (!checkStatus(data.Status)) {
        html += "<li>" + data.Message + "</li>"
    } else {
        $(data.Data).each(function () {
          var allcookies = document.cookie;
          var open = allcookies.substring(5);

            var currentUrl = "index.php?openid="+open+"&did=" + this[0] + "&gatename=" + this[1];
            html += "<li><a href=\"" + currentUrl + "\">" + this[1] + "</a></li>"
        })
    }
    html = "<ul>" + html + "</ul>"
    // console.log(html);
    $("#menubar").html(html);
}
function loadHommer(data) {
  var weatherHTML = "<td class=\"tdrights\">当地天气：</td>";
    if (!checkStatus(data.Status)) {
          weatherHTML += "<td class=\"tdrights bluetext\">无数据</td>";
          $("#txtHommer").text("燕子不在线");
          $("#txtHommer").css("color" , 'red');

    } else {
      // console.log(data);
        $("#txtHommer").text("燕子在线");
        $("#txtHommer").css("color" , '#14bb9f');
        weatherHTML += "<td class=\"tdrights bluetext\">" + data.temperature+ "℃ " + "</td>";
        weatherHTML += "<td class=\"tdright\">" + data.weather+'&nbsp&nbsp&nbsp&nbsp' + data.provcn+ data.namecn +"</td>";
        // weatherHTML += "<td class=\"tdleft\">" + data.provcn + " "  + "</td>";
    }
    $("#weather").html(weatherHTML);
}
///////////////////
// function bloodpressureLoad() {
//     var bloodpressureHTML = "<td class=\"tdright\">心率血压：</td>";
//     var heartrateHTML = "<td class=\"tdright\">心&nbsp;&nbsp;&nbsp;&nbsp;率：</td>";
//     if (checkStatus(result.Status567)) {
//         var model = result.Data567;
//         // console.log(model);
//         var time = _baseformat.formatTime(model[0]);
//         bloodpressureHTML += "<td class=\"tdleft bluetext\">" +model[3] + "/" + model[1] + "/" + model[2] + "</td>";
//         bloodpressureHTML += "<td>" + time + "</td>";
//
//     } else {
//         bloodpressureHTML += "<td class=\"tdleft\">无数据</td>";
//     }
//     $("#heartrate").html(heartrateHTML);
//     $("#bloodpressure").html(bloodpressureHTML);
// }
/////////////////////////////
